<?php 
ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
require_once EXTEND_PATH."weixinpay/lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
class Wxjspay{
    private $configs=[];
    private $httpsUrl = "";
    function __construct($configs){
        $this->configs=$configs;
    }
    function setUrls($url=""){
        $this->httpsUrl=$url;
    }
    
    public function builderPay($openId,$orderID,$moneys,$coupon=""){

        //①、获取用户openid
        $tools = new JsApiPay();
        
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetAppid($this->configs['APPID']);
        $input->SetMch_id($this->configs['MCHID']);
        $input->SetBody("订单支付");
        $input->SetAttach($coupon);
        $input->SetOut_trade_no($orderID);
        $input->SetTotal_fee("".($moneys*100));
        //$input->SetTotal_fee("1");
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("test");
        $input->SetNotify_url($this->httpsUrl."/api/v1.Payres/orderpayresult");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($this->configs,$input);
        $jsApiParameters = $tools->GetJsApiParameters($order,$this->configs);
        return $jsApiParameters;
    }
    
    public function buildervipPay($openId,$datas){
    
        //①、获取用户openid
        $tools = new JsApiPay();
    
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetAppid($this->configs['APPID']);
        $input->SetMch_id($this->configs['MCHID']);
        $input->SetBody("会员充值");
        $input->SetAttach(json_encode($datas));
        $input->SetOut_trade_no(date("YmdHis"));
        $input->SetTotal_fee("".($datas['rechargeamount']*100));
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("test");
        $input->SetNotify_url($this->httpsUrl."/api/v1.Payres/vippayresult");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($this->configs,$input);
        $jsApiParameters = $tools->GetJsApiParameters($order,$this->configs);
        $sss['data'] = $jsApiParameters;
        $sss['out_trade_no'] = $input->GetOut_trade_no();
        return $sss;
    }
    
    
    function builderRefund($orderid,$money){
        $input = new WxPayRefund();
        $input->SetAppid($this->configs['APPID']);
        $input->SetMch_id($this->configs['MCHID']);
        $input->SetOut_trade_no($orderid);
        $input->SetTotal_fee($money*100);
        $input->SetRefund_fee($money*100);
        $input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
        $input->SetOp_user_id(WxPayConfig::MCHID);
        return WxPayApi::refund($this->configs,$input);
    }
    
    
}

